﻿package de.gameduell.framework.facebook.app.pages {
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.facebook.FBBasePageManager;
	import de.gameduell.framework.facebook.Page;
	import de.gameduell.framework.facebook.communication.FacebookData;
	import de.gameduell.framework.gui.button.Button;
	import de.gameduell.framework.gui.button.ButtonEvent;
	import de.gameduell.framework.gui.util.ToolTip;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;

	public class StatusPage extends Page {
		private var cashButton:Button;
		private var balanceField:TextField;
		private var balanceClip:MovieClip;
		private var lifeCount:TextField; 
		private var livesIcon:Sprite;

		public function StatusPage(aPageManager:FBBasePageManager, anId:String){
			super(aPageManager, anId);
		}

		override protected function initPage():void {
			lifeCount = page.getChildByName("lifeCount") as TextField;
			lifeCount.visible = false;
			
			cashButton = new Button(page.getChildByName("cashButton") as Sprite, "status_cashButton");
			cashButton.addEventListener(ButtonEvent.CLICK, controller.openPaymentPopup);
			
			balanceClip = page.getChildByName("balanceClip") as MovieClip;
			balanceField = balanceClip.getChildByName("balanceField") as TextField;
			balanceField.text = controller.facebookData.balance != null ? controller.facebookData.balance.balance : "-";
			
			controller.facebookData.addEventListener(FacebookData.FBE_USER_BALANCE, updateBalance);
			updateBalance();
			
			new ToolTip("tooltip", "status_balance_tooltip", balanceClip, true);
			livesIcon = page.getChildByName("livesIcon") as Sprite;
			if(livesIcon != null){
				new ToolTip("tooltip", "status_lives_tooltip", livesIcon, true);
			}
		}
		
		private function updateBalance(event:Event = null):void {
			Debug.trace("StatusPage::updateBalance", Debug.DEBUG);
			if(controller.facebookData.balance != null){
				balanceField.text = controller.facebookData.balance.balance;
				balanceClip.gotoAndPlay("blink");
				lifeCount.visible = true;
			}
		}
	}
}